package com.xqq.minio;



import com.xqq.utils.BytesTransformUtil;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.List;

@Slf4j
@Service
public class MinioClientManagerImpl implements MinioClientManager {

    private static String prefix = "wh_image_";

    @Override
    public String uploadBase64Image(String base64Image, String bucketName, MinioClient client) {
        // 去掉base64头部
        if (base64Image.contains(",")) {
            base64Image = base64Image.split(",")[1];
        }

        // 解码Base64字符串
        byte[] imageBytes = Base64.getDecoder().decode(base64Image);
        InputStream imageStream = new ByteArrayInputStream(imageBytes);
        try {
            String fileObjectId = getFileObjectId(imageBytes);
            // 上传到MinIO
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileObjectId)
                    .stream(imageStream, imageBytes.length, -1)
                    .contentType("image/jpeg")
                    .build();
            client.putObject(putObjectArgs);
            log.info("图片{}写入Minio成功", fileObjectId);
            return fileObjectId;
        }catch (MinioException e) {
            log.error("MinIO异常:{} ",e.getMessage());
            return null;
        } catch (Exception e) {
            log.error("其它异常:{} ", e.getMessage());
            return null;
        }  finally {
            try {
                if (imageStream != null) {
                    imageStream.close();
                }
            } catch (IOException e) {
                log.error("输入流关闭异常: ", e);
            }
        }

    }



    @Override
    public String saveImageContentToMinio(MinioClient client,
                                          String bucketName,
                                          MultipartFile multipartFile) {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
            // 使用PutObjectArgs构建PutObject请求，调用putObject方法将图片保存到MinIO
            byte[] imageContent = multipartFile.getBytes();
            String fileObjectId = getFileObjectId(imageContent);
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(getFileObjectId(imageContent))
                    .stream(multipartFile.getInputStream(), imageContent.length, -1)
                    .contentType("image/jpeg")
                    .build();
            client.putObject(putObjectArgs);
            log.info("图片{}写入Minio成功", fileObjectId);
            return fileObjectId;
        } catch (MinioException e) {
            log.error("MinIO异常:{} ",e.getMessage());
            return null;
        } catch (Exception e) {
            log.error("其它异常:{} ", e.getMessage());
            return null;
        }  finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("输入流关闭异常: ", e);
            }
        }
    }

    private static final int THREAD_POOL_SIZE = 10; // 设置线程池大小
    @Override
    public String batchSaveImageContentToMinio(MinioClient minioClient,
                                               String bucketName,
                                               List<MultipartFile> multipartFiles) {
        return null;
    }

    /**
     * 设置存储在Minio的对象文件名
     */
    private static String getFileObjectId(byte[] imageData) {
        return prefix + BytesTransformUtil.generateIdFromImage(imageData) + ".jpg";
    }
}
